package test.wechat.handler;

import java.util.Date;

import lodsve.wechat.api.message.EventHandler;
import lodsve.wechat.beans.message.EventType;
import lodsve.wechat.beans.message.ReplyType;
import lodsve.wechat.beans.message.receive.event.SubscribeEvent;
import lodsve.wechat.beans.message.reply.Reply;
import lodsve.wechat.beans.message.reply.TextReply;
import org.springframework.stereotype.Component;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 16/2/27 上午2:24
 */
@Component
public class SubscribeEventHandler implements EventHandler<SubscribeEvent> {
    @Override
    public boolean support(EventType eventType) {
        return eventType == EventType.subscribe || eventType == EventType.unsubscribe;
    }

    @Override
    public Reply handle(SubscribeEvent event) {
        TextReply reply = new TextReply();
        reply.createTime = new Date().getTime();
        reply.fromUserName = event.toUserName;
        reply.toUserName = event.fromUserName;
        reply.msgType = ReplyType.text;

        if (EventType.subscribe == event.event) {
            // 关注事件
            reply.content = "感谢关注,我是孙昊!";
        } else if (EventType.unsubscribe == event.event) {
            // 取消关注事件
            reply.content = "不要嘛...";
        }
        return reply;
    }

    @Override
    public Class<SubscribeEvent> getType() {
        return SubscribeEvent.class;
    }
}
